﻿
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Traceability.Infrastructure;

namespace Traceability.Repository
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class, new()
    {
        /// <summary>
        /// 数据库上下文
        /// </summary>
        private readonly TraceabilitiesDbContext _db;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<BaseRepository<T>> _logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"> 上下文   </param>
        /// <param name="logger">  日志  </param>
        public BaseRepository(TraceabilitiesDbContext db, ILogger<BaseRepository<T>> logger)
        {
            this._db = db;
            _logger = logger;
        }
        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns></returns>
        public async Task<IList<T>> GetAllAsync()
        {
            try
            {
                return await _db.Set<T>().ToListAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError("查询所有数据失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <param name="expression">lambda表达式</param>
        /// <returns> 返回符合条件的数据集合 </returns>
        public async Task<IList<T>> GetWhereAsync(Expression<Func<T, bool>> expression)
        {
            try
            {
                return await _db.Set<T>().Where(expression).ToListAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError("查询所有数据失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="entity"> 实体</param>
        /// <returns>返回是否成功 </returns>
        public async Task<int> InsertDataAsync(T entity)
        {
            try
            {
                await _db.Set<T>().AddAsync(entity);
                return await _db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError("操作失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 获取单个对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns> 返回符合条件的数据集合 </returns>
        public async Task<T> GetAsync(int id)
        {
            try
            {
                var T = await _db.Set<T>().FindAsync(id);

                if (T == null)
                {
                    _logger.LogError("查询数据失败");
                    return new T();
                }

                return T;
            }
            catch (Exception ex)
            {
                _logger.LogError("查询数据失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 获取单个对象(通过条件)
        /// </summary>
        /// <param name="id"></param>
        /// <returns>返回符合条件的数据集合 </returns>
        public async Task<T> GetAsync(Expression<Func<T, bool>> expression)
        {
            try
            {
                var T = await _db.Set<T>().Where(expression).FirstOrDefaultAsync();

                if (T == null)
                {
                    _logger.LogError("查询数据失败");
                    return new T();
                }

                return T;
            }
            catch (Exception ex)
            {
                _logger.LogError("查询数据失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> UpdateDataAsync(T entity)
        {
            try
            {
                _db.Set<T>().Update(entity);
                return await _db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError("操作失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> DeleteDataAsync(T entity)
        {
            try
            {
                _db.Set<T>().RemoveRange(entity);
                return await _db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError("操作失败" + ex.Message);
                throw;
            }
        }
    }
}
